﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace calculator
{
    public class TruthTable
    {
        private string name;
        private TruthTableRow[] table;
        public TruthTable()
        {
            table = new TruthTableRow[0];
            name = "Unamed";
        }
        public TruthTable(string name)
        {
            table = new TruthTableRow[0];
            this.name = name;
        }
       
        public string getName()
        {
            return name;
        }
        public int getLength()
        {
            return table.Length;
        }
        //fall sem afritar núverandi stök, bætir við einu og svo setur nýja stakið inn
        public void addRow(TruthTableRow t)
        {
            TruthTableRow[] tempTable = new TruthTableRow[table.Length + 1];
            for (int i = 0; i < table.Length; i++)
            {
                tempTable[i] = table[i];
            }
            tempTable[table.Length] = t;
            table = tempTable;
        }

        public TruthTableRow getRow(int rowNumber)
        {
            if (rowNumber < 0 || rowNumber > this.getLength())
                throw new OverflowException("Row does not exist");
            else
                return table[rowNumber];
        }

        public TruthTableRow findRow(int searchId)
        {
            for (int i = 0; i < table.Length; i++)
            {
                if (table[i].getId() == searchId)
                    return table[i];
            }
            return null;
        }
        public TruthTableRow findRow(string searchString)
        {
            for (int i = 0; i < table.Length; i++)
            {
                if (table[i].getName() == searchString)
                    return table[i];
            }
            return null;
        }

        public static bool operator ==(TruthTable a, TruthTable b)
        {
            if (a.name != b.name)
                return false;
            if (a.table.Length != b.table.Length)
                return false;
            for (int i = 0; i < a.table.Length; i++)
            {
                if (a.table[i] != b.table[i])
                    return false;
            }
            return true;
        }
        public static bool operator !=(TruthTable a, TruthTable b)
        {
            return !(a == b);
        }





    }
}
